﻿<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>

<script type="text/javascript">
	var sparepart = {
		gridId : "sparepartBackDetailGrid",
		saveUrl : "sparepart/sparepartChangeDetail/save"
	};
</script>

<script type="text/javascript" src="js/widget/sparepart.js"></script>

<form:form id="sparepartBackForm" commandName="model"
	cssClass="validate" action="sparepart/sparepartBack/save">
	<form:hidden path="id" />
	<isperp:processProcesser processId="${model.processId }"></isperp:processProcesser>
	<table class=" form-table">
		<tr>
			<td class="td1">编号：</td>
			<td class="td2"><form:input path="code" disabled="true" /><span>
					(自动生成)</span></td>
			<td class="td1">归还日期：</td>
			<td class="td2"><form:input type="date" path="backDate"
					cssClass="required" /></td>
		</tr>
		<tr>
			<td class="td1">归还人：</td>
			<td class="td2"><form:hidden path="backManId" /> <isperp:departmentUser
					id="backManName" displayValue="${model.backManName }"
					afterSelected="afterSelected(row);" displayValueCssClass="required"
					name="backManName" valueControlId="backManId"></isperp:departmentUser>
			</td>
			<td class="td1">项目：</td>
			<td class="td2"><form:select path="projectId"
					items="${projects }" itemLabel="name" itemValue="id"
					cssClass="search"></form:select></td>
		</tr>
		<tr>
			<td class="td1">描述：</td>
			<td class="td2" colspan="3"><form:textarea path="memo"
					cssClass="required" /> </textarea>
			</td>
		</tr>
		<tr>
			<td class="td1">填表人：</td>
			<td class="td2"><label>${creatorName }</label></td>
		</tr>
		<tr>
			<td class="td1">附件：</td>
			<td class="td2" colspan="3"><isperp:uploader
					sourceId="${model.id }" id="sptFiles"></isperp:uploader>
			</td>
		</tr>
	</table>
</form:form>

<isperp:grid
	url="sparepart/sparepartRelation/listSparepartCi?sparepartId=${model.id }"
	gridId="sparepartInCiDetailGrid" showPager="false"
	showSearchbar="false" cusDialogSize="big"
	cusDeleteUrl="sparepart/sparepart/plan/delete?sparepartId=${model.id }"
	cusEditUrl="sparepart/sparepart/plan/edit?ty=sparepartOut&sparepartId=${model.id }"
	cusFormatterActions="true">
	<jsp:attribute name="toolbar">
		<isperp:toolbar id="iconToolbar">
		   <div style="float:left;" class="header">
					<h3>归还设备清单</h3>
				</div>	
			<isperp:button text="选择归还资产" icon="icon-plus" onclick="choseCi();"></isperp:button>
		</isperp:toolbar>
		</jsp:attribute>
	<jsp:body>
		<isperp:column name="id" title="id" hidden="true"></isperp:column>
		<isperp:column name="code" title="编号" ></isperp:column>
	    <isperp:column name="name" title="名称" ></isperp:column>
	    <isperp:column name="category_name" title="分类" ></isperp:column>
	    <isperp:column name="devide_number" title="设备号/PN号" ></isperp:column>
	    <isperp:column name="location" title="位置" ></isperp:column>
	    <isperp:column name="status" title="状态" ></isperp:column>
	    <isperp:column name="support_staff_name" title="使用人" ></isperp:column>
	</jsp:body>
</isperp:grid>

<div class="header">
	<h3>关联流程</h3>
</div>

<isperp:processRelation businessKey="${model.id }" readonly="false"
	processKey="sparepartBackProcess" projectIdControl="projectId"
	showTotalControl="relationProcessCount"></isperp:processRelation>

<isperp:buttonList>
	<isperp:processButtons processKey="sparepartBackProcess"
		processId="${model.processId }" businessKey="${model.id }"
		beforeSubmit="function(name){return check(name);}"
		formId="sparepartBackForm"></isperp:processButtons>
</isperp:buttonList>

<isperp:listSelector id="ciRelateSelector" displayValueCssClass="hidden"
	showClearBtn="false" selectorCssClass="hidden" reloadOnOpen="true"
	urlExtScript=" '&cusUserName='+($('#backManId').val())"
	url="sparepart/sparepart/listDetail?sourceId=${model.id }&ty=sparepartBack&fromId=${fromId }&fromProcessKey=${fromProcessKey }"
	afterSelected="gird_afterSelected(rows)" multiselect="true">
	<isperp:column name="id" title="id" hidden="true"></isperp:column>
	<isperp:column name="code" title="编号" width="80">
	</isperp:column>
	<isperp:column name="name" title="名称" width="100">
	</isperp:column>
	<isperp:column name="status" title="状态" width="60">
	</isperp:column>
</isperp:listSelector>

<script type="text/javascript">
	function choseCi() {
		if ($("#useManName").val() == "") {
			isperp.alert('请选择归还人！');
			return false;
		}
		$("#ciRelateSelector").click();
	}
	
	$(function(){
	   var useProjectId='${model.projectId}';
	   if(!useProjectId)
	   {
	      $("#projectId").val("").trigger("chosen:updated");
	   }
	});
	function gird_afterSelected(rows) {
		if (rows.length < 1) {
			isperp.alert("没有数据被选择！")
			return;
		}
		var ids = "";
		for ( var i = 0; i < rows.length; i++) {
			if (i == 0) {
				ids = rows[0].id;
			} else {
				ids += "," + rows[i].id;
			}
		}
		$.ajax({
			url : "sparepart/sparepartRelation/saveRelation",
			data : {
				sparepartId : '${model.id}',
				ciId : ids,
				typeKey : 'sparepartBack'
			},
			success : function(data) {
				sparepartInCiDetailGrid.search();
			},
			error : function(data) {
				isperp.alert(data)
			}
		});

	}
	function afterSelected(row) {

		/* $.getJSON("project/member/listProjectByUsername?username=" + row[0], function(data) {

			$("#projectId").empty();
			if (data[0]) {
				$(data).each(function(index, item) {
					$("#projectId").append('<option value="'+item.id+'">' + item.name + '</option>');
				});
			}
			$("#projectId").trigger("chosen:updated");
		}); */
	}
	var sub_result = false;
	function check(name) {
		var relationProRows = $("#sparepartInCiDetailGrid").jqGrid("getRowData");
		if (name == "提交" && sub_result==false && relationProRows.length<1) {
		    if($("#sparepartBackForm").valid()==false)
			{
			   return false;
			}
			
			isperp.confirmAlert("当前没有选择归还备件，是否提交？", {
				yes : function() {
					//提交表单
					sub_result = true;
					$("[data-name='提交']").click();

				}
			});
		} else {
			return true;
		}

	}
</script>